<template>
    <div class="container">

        <!-- 搜索区域 -->
        <el-row>
            <el-form :inline="true">
                <el-form-item label="公告名称">
                    <el-input v-model="queryInfo.name" clearable @clear="queryList" placeholder="请输入公告名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="search">搜索</el-button>
                    <el-button type="primary" @click="toAddPublicity">新增公告</el-button>
                </el-form-item>
            </el-form>
        </el-row>
        <!-- 表格区域 -->
        <el-table :data="publicityList" border style="width:100%">
            <!-- <el-table-column type="selection" width="55"></el-table-column> -->
            <el-table-column prop="id" label="ID"></el-table-column>
            <el-table-column prop="name" label="公告名称">
            </el-table-column>
            <el-table-column prop="publicityText" label="公告内容">
                <template slot-scope='scop'>
                    <el-link type='primary' @click="look(scop.row)">点击查看</el-link>
                </template>
            </el-table-column>
            <el-table-column prop="createTime" label="创建时间">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope='scope'>
                    <el-link type="primary" @click="toEditPublicity(scope.row)">编辑公告</el-link>
                    <el-link type="primary" @click="toDeletePublicity(scope.row.id)">删除公告</el-link>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="queryInfo.pageNum" :page-sizes="[1, 4, 6, 8, 10]" :page-size="queryInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        <!-- 新增和修改的对话框 -->
        <!-- dialog中绑定close事件，实现对话框右上角叉号的取消功能 -->
        <el-dialog :title='title' :visible.sync="dialogFormVisible" @close="cancel">
            <el-form :model="publicity">
                <!-- <el-form-item label="ID" label-width="80px">
                    <el-input placeholder="请输入ID" v-model="publicity.id"></el-input>
                </el-form-item> -->
                <el-form-item label="公告名称" label-width="80px">
                    <el-input placeholder="请输入公告名称" v-model="publicity.name"></el-input>
                </el-form-item>
                <!-- <el-form-item label="公告内容" label-width="80px">
                    <el-input placeholder="请输入公告内容" v-model="publicity.publicityText"></el-input>
                </el-form-item> -->
                <el-form-item label="公告内容" label-width="80px">
                    <!-- 富文本编辑器组件 -->
                    <quill-editor v-model="publicity.publicityText"></quill-editor>
                </el-form-item>
                <el-form-item label="发布时间" label-width="80px">
                    <el-input placeholder="请输入发布时间" v-model="publicity.createTime"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="addPublicity">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 点击查看公告内容 -->
        <el-dialog :title='title' :visible.sync="dialog" @close="cancel">
            <el-form :model="publicity">
                <!-- <el-form-item label="ID" label-width="80px">
                    <el-input placeholder="请输入ID" v-model="publicity.id"></el-input>
                </el-form-item> -->
                <!-- <el-form-item label="公告内容" label-width="80px">
                    <el-input placeholder="请输入公告内容" v-model="publicity.publicityText"></el-input>
                </el-form-item> -->
                <el-form-item label="公告内容" label-width="80px">
                    <!-- 富文本编辑器组件 -->
                    <quill-editor v-model="publicity.publicityText"></quill-editor>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="can">关闭查看</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data() {
        return {
            dialogVisible: false, // 预览图片的对话框
            // 查询信息对象
            queryInfo: {
                name: '',
                pageNum: 1,
                pageSize: 4
            },
            total: 0,
            publicityList: [],
            title: '添加', // 对话框标题
            dialogFormVisible: false, // 是否显示对话框
            dialog:false,  //公告内容先不显示
            publicity: {
                // 文章对象
                id: "",
                name: "",
                publicityText: "",
                createTime: "",
            },
            fileList: [],
        }
    },
    methods: {
     
     
        // 添加文章
        addPublicity() {
            if (this.publicity.id) { // 有值为true  修改
                // .........后面实现..............
                this.$http.post("publicity/update", this.publicity)
                    .then(resp => {
                        if (resp.data.status == 200) {
                            // 消息提示
                            this.$message.success('修改成功！');
                            // 清空数据
                            this.publicity = {
                                // 文章对象
                                "id": '',
                                "name": "",
                                "publicityText": "",
                                "createTime": "",
                            }
                            this.fileList = []
                            // 关闭对话框
                            this.dialogFormVisible = false;
                            // 重新查询
                            this.queryList()
                        } else {
                            this.$message.error('接口异常！')
                        }
                    })
            } else {
                this.$http.post("publicity/insert", this.publicity)
                    .then(resp => {
                        if (resp.data.status == 200) {
                            // 消息提示
                            this.$message.success('添加成功！');
                            // 清空数据
                            this.publicity = {
                                // 文章对象
                                "id": '',
                                "name": "",
                                "publicityText": "",
                                "createTime": "",
                            }
                            this.fileList = []
                            // 关闭对话框
                            this.dialogFormVisible = false;
                            // 重新查询
                            this.queryList()
                        } else {
                            this.$message.error('接口异常！')
                        }
                    })
            }
        },
        // 删除文章
        toDeletePublicity(id) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    this.$http.delete('publicity/delete/' + id)
                        .then(resp => {
                            if (resp.data.status == 200) {
                                this.$message.success(resp.data.message)
                                this.queryList()
                            } else {
                                this.$message.error(resp.data.message)
                            }
                        })
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })
        },
        // 进入修改的预览数据页面
        toEditPublicity(row) {
            //console.log(row)
            // 赋值
            this.publicity = row;
            // 设置标题
            this.title = "修改公告";
            // 开启对话框
            this.dialogFormVisible = true;

        },
           //点击查看公告内容
           look(row){
            //赋值
            this.publicity=row;
            this.title="查看公告内容";
            this.dialog=true;
},
        /// 取消对话框
        cancel() {
            this.dialogFormVisible = false;
            this.publicity = {
                // 文章对象
                "id": '',
                "name": "",
                "publicityText": "",
                "createTime": "",
            }
            this.fileList = []
        },
        can(){
            this.dialog=false;
        },
        search() {
            this.queryInfo.pageNum = 1;
            this.queryList();
        },
        // 进入添加的对话框
        toAddPublicity() {
            this.title = "新增公告";
            this.dialogFormVisible = true;
        },
        handleSizeChange(size) {
            this.queryInfo.pageSize = size;
            console.log("size" + size)

            this.queryList();
        },
        handleCurrentChange(num) {
            this.queryInfo.pageNum = num;
            console.log('num:' + num)

            this.queryList();
        },
        // 分页查询信息的方法
        queryList() {
            console.log(this.queryInfo);
            // 调用后端接口来获取数据
            this.$http.get('publicity/findByPage', {
                params: this.queryInfo
            })
                .then(resp => {
                    if (resp.data.status == 200) {
                        console.log(resp.data)
                        this.publicityList = resp.data.data.list;
                        this.total = resp.data.data.total;
                        console.log(this.publicityList)
                    } else {
                        this.$message.error('查询失败！')
                    }
                })
        }
    },
    mounted() {
        this.queryList();
    }
}
</script>
<style>
/**设置富文本编辑器的最小高度，注意style中不能使用scoped **/
.ql-container {
    min-height: 200px;
}

.el-breadcrumb {
    margin-bottom: 15px;
    padding: 0 0 15px;
    border-bottom: 1px solid #ddd;
}

.el-pagination {
    margin-top: 15px;
}

.el-link {
    padding: 0 5px;
}
</style>